package cn.com.nxt.txb;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import cn.com.nxt.txb.util.AppConstant;
import cn.com.nxt.txb.util.AppManager;
import cn.com.nxt.txb.widget.LoadingDialog;

/**
 * Created by lt on 2017/5/19.
 */

public class BaseActivity extends AppCompatActivity implements HttpProtocol, ServiceConnection {
    private LoadingDialog dialog = null;
    public Toast mToast = null;
    public SharedPreferences.Editor editor;
    public SharedPreferences sp;
    public MQTTService.MQTTBinder mqttBinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences(AppConstant.USERINFO, MODE_PRIVATE);
        editor = sp.edit();
        AppManager.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity从堆栈中移除
        AppManager.getAppManager().finishActivity2(this);
    }

    public static void showLog(String msg) {
        if (BuildConfig.DEBUG) {
            Log.e("sdydbj", msg);
        }
    }

    public void showToast(String msg) {
        if (BuildConfig.DEBUG) {
            if (mToast == null) {
                mToast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
            mToast.show();
        }
    }
    public void showToast(int msg) {
        if (BuildConfig.DEBUG) {
            if (mToast == null) {
                mToast = Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT);
            } else {
                mToast.setText(msg);
                mToast.setDuration(Toast.LENGTH_SHORT);
            }
            mToast.show();
        }
    }


    @Override
    public void success(String result, byte flag) {

    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {

    }

    @Override
    public void finished(int flag) {

    }

    @Override
    public void cancelled(int flag) {

    }

    public void back(View v) {
        finish();
    }

    // 环形加载进度条
    public void showDialog() {
        if (dialog != null) {
            dialog = null;
        }
        dialog = new LoadingDialog(this);
        dialog.setCancelable(true);
        dialog.show();
    }

    public void showDialog(String msg) {
        dialog = new LoadingDialog(this, msg);
        dialog.show();
    }

    public void disDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        mqttBinder = (MQTTService.MQTTBinder) service;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

}
